package com.ok.pay.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ok.api.client.PrizeClient;
import com.ok.api.client.UserClient;
import com.ok.api.domain.po.OkAddr;
import com.ok.api.domain.po.OkPrize;
import com.ok.api.domain.po.OkUser;
import com.ok.common.domain.po.Result;
import com.ok.common.utils.UserContext;
import com.ok.pay.domain.dto.OkOrderDTO;
import com.ok.pay.domain.po.OkOrder;
import com.ok.pay.domain.vo.OkOrderVO;
import com.ok.pay.mapper.OkOrderMapper;
import com.ok.pay.service.IOkOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.reflections.util.Utils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author lhw
 * @since 2024-05-06
 */
@Service
public class OkOrderServiceImpl extends ServiceImpl<OkOrderMapper, OkOrder> implements IOkOrderService {

    @Autowired
    private UserClient userClient;
    @Autowired
    private PrizeClient prizeClient;

    @Override
    public void addOrder(OkOrderDTO okOrderDTO) {
        //构建订单信息
        OkOrder okOrder = new OkOrder();
        BeanUtils.copyProperties(okOrderDTO,okOrder);
        long orderName = IdWorker.getId();    //获取订单号--mp提供
        okOrder.setOrderName(String.valueOf(orderName));
        okOrder.setUserId(UserContext.getUser());

        //向订单表中添加数据
        this.save(okOrder);
    }

    @Override
    public Page pageWith(Integer pageNo, Integer pageSize, String orderName) {
        //分页查询订单表
        Page<OkOrder> orderPage = new Page<>(pageNo, pageSize);
        LambdaQueryWrapper<OkOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(!Utils.isEmpty(orderName),OkOrder::getOrderName,orderName);
        Page<OkOrder> page = this.page(orderPage, queryWrapper);

        //封装Vo返回
        Page<OkOrderVO> voPage = new Page<>();
        //对象拷贝--除开records
        BeanUtil.copyProperties(page,voPage,"records");

        List<OkOrder> records = page.getRecords();

        //通过stream流获得新records
        List<OkOrderVO> list = records.stream().map((item)->{
            OkOrderVO orderVO = new OkOrderVO();
            BeanUtil.copyProperties(item,orderVO);
            //得到用户id封装用户名
            Result<OkUser> info = userClient.info(item.getUserId());
            orderVO.setUsername(info.getData().getUsername());
            //封装奖品名称
            Result<OkPrize> prizeInfo = prizeClient.prizeInfo(item.getPrizeId());
            orderVO.setPrizeName(prizeInfo.getData().getName());
            //的到地址id封装收货信息
            Result<OkAddr> infoAddr = userClient.infoAddr(item.getAddrId());
            orderVO.setAddr(infoAddr.getData().getProvince()+infoAddr.getData().getCity()+infoAddr.getData().getTown()+infoAddr.getData().getStreet());
            orderVO.setUName(infoAddr.getData().getContact());
            orderVO.setPhone(infoAddr.getData().getPhone());
            return orderVO;
        }).collect(Collectors.toList());

        //添加voPage的records
        voPage.setRecords(list);
        return voPage;
    }
}
